package com.bba.manager.as.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bba.manager.as.mapper.RepairMapper;
import com.bba.manager.as.service.IRepairPartsService;
import com.bba.manager.as.service.IRepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bba.manager.common.model.as.DTO.RepairDTO;
import com.bba.manager.common.model.as.Repair;
import com.bba.manager.common.model.as.RepairParts;
import com.bba.manager.common.model.as.VO.repairQuery;
import com.bba.manager.common.model.user.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 汽车维修表 服务实现类
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-12
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements IRepairService {
    @Autowired
    private IRepairPartsService repairPartsService;

    @Override
    public IPage listByEmployee(int employeeId, Integer current, Integer size) {
        LambdaQueryChainWrapper<Repair> query = new LambdaQueryChainWrapper<>(this.baseMapper);
        IPage<Repair> page = new Page<>(current, size);
        return query.eq(Repair::getEmployeeId, employeeId).page(page);
    }

    @Override
    public void updateStatus(Long repairNo, String status) {
        LambdaQueryChainWrapper<Repair> query = new LambdaQueryChainWrapper<>(this.baseMapper);
        Repair one = query.eq(Repair::getRepairNo, repairNo).one();
        one.setStatus(status);
        this.baseMapper.updateById(one);
    }

    @Override
    public void updateType(Long repairNo, String type) {
        LambdaQueryChainWrapper<Repair> query = new LambdaQueryChainWrapper<>(this.baseMapper);
        Repair one = query.eq(Repair::getRepairNo, repairNo).one();
        one.setType(type);
        this.baseMapper.updateById(one);
    }

    @Override
    public RepairDTO getRepairDTO(Long repairNo) {
        LambdaQueryChainWrapper<Repair> query = new LambdaQueryChainWrapper<>(this.baseMapper);
        Repair one = query.eq(Repair::getRepairNo, repairNo).one();
        RepairDTO repairDTO = new RepairDTO();
        repairDTO.setRepair(one);
        List<RepairParts> repairParts = repairPartsService.ListById(repairNo);
        repairDTO.setRepairList(repairParts);
        BigDecimal total = repairParts.stream()
                .map(e -> e.getPrice().multiply(new BigDecimal(e.getNumber())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        repairDTO.setTotal(total);
        return repairDTO;
    }

    @Override
    public IPage listByInfo(repairQuery query) {
        Page<Repair> page = new Page<>(query.getPage(), query.getLimit());
        return this.page(page);
    }


}
